package com.febers.ihealth.fragment

import android.view.View
import android.widget.CompoundButton
import com.febers.ihealth.R
import com.febers.ihealth.base.BaseFragment
import com.febers.ihealth.entity.ReleaseNextSurveyEvent
import com.febers.ihealth.utils.Event
import com.febers.ihealth.utils.log
import com.febers.ihealth.utils.user
import kotlinx.android.synthetic.main.survey_kesou_toutong_liuti.*

class SurveyKesouToutengLiuti: BaseFragment() {

    private var chooseKesou = false
    private var chooseToutong = false
    private var chooseShentong = false
    private var chooseBizi = false
    private var chooseQichuan = false

    private var chooseYanhou = false
    private var chooseYanhouCount = 0
        set(value) {
            field = value
            chooseYanhou = field > 0
        }


    override fun setView(): Int = R.layout.survey_kesou_toutong_liuti

    override fun initView() {
        //如果有咳嗽
        if (!user.kesouQichuan) {
            tv_title_kesou.visibility = View.GONE
            radio_button_ketan_qingxi.visibility = View.GONE
            radio_button_ketan_huangchou.visibility = View.GONE
            radio_button_baitan_yikechu.visibility = View.GONE
            radio_button_tanshao_nankechu.visibility = View.GONE
            radio_button_wutan.visibility = View.GONE
            radio_group_qichuan.visibility = View.GONE
            chooseKesou = true
            chooseQichuan = true
        } else {
            //点击有气喘或者无气喘
            radio_group_qichuan.setOnCheckedChangeListener { group, checkedId ->
                when(checkedId) {
                    R.id.radio_btn_youqichuan -> {
                        //咳痰最多选一个
                        radio_button_ketan_qingxi.clearCheck()
                        radio_button_ketan_huangchou.clearCheck()
                        radio_button_baitan_yikechu.clearCheck()
                        radio_button_tanshao_nankechu.clearCheck()
                        radio_button_wutan.clearCheck()
                        chooseKesou = false
                        chooseQichuan = true
                        user.youqichuan = true
                        user.wuqichuan = false
                        Event.post(ReleaseNextSurveyEvent(false))
                    }
                    R.id.radio_btn_wuqichuan -> {
                        //咳痰最少选一个
                        chooseQichuan = true
                        user.youqichuan = false
                        user.wuqichuan = true
                        if (chooseYanhou && chooseKesou && chooseBizi && chooseToutong && chooseShentong && user.checkToutongFinish()) {
                            Event.post(ReleaseNextSurveyEvent(true))
                        }
                    }
                }
            }
            radio_button_ketan_qingxi.setOnCheckedChangeListener { buttonView, isChecked ->
                user.ketanQingxi = isChecked
                if (user.youqichuan) {
                    radio_button_ketan_huangchou.clearCheck()
                    radio_button_baitan_yikechu.clearCheck()
                    radio_button_tanshao_nankechu.clearCheck()
                    radio_button_wutan.clearCheck()
                }
                chooseKesou = isChecked
                if (chooseYanhou && chooseQichuan && chooseBizi && chooseToutong && chooseShentong && user.checkToutongFinish()) {
                    Event.post(ReleaseNextSurveyEvent(true))
                }
            }
            radio_button_ketan_huangchou.setOnCheckedChangeListener { buttonView, isChecked ->
                user.ketanHuangchou = isChecked
                if (user.youqichuan) {
                    radio_button_ketan_qingxi.clearCheck()
                    radio_button_baitan_yikechu.clearCheck()
                    radio_button_tanshao_nankechu.clearCheck()
                    radio_button_wutan.clearCheck()
                }
                chooseKesou = isChecked
                if (chooseYanhou && chooseQichuan && chooseBizi && chooseToutong && chooseShentong && user.checkToutongFinish()) {
                    Event.post(ReleaseNextSurveyEvent(true))
                }
            }
            radio_button_baitan_yikechu.setOnCheckedChangeListener { buttonView, isChecked ->
                user.yiKechu = isChecked
                if (user.youqichuan) {
                    radio_button_ketan_qingxi.clearCheck()
                    radio_button_ketan_huangchou.clearCheck()
                    radio_button_tanshao_nankechu.clearCheck()
                    radio_button_wutan.clearCheck()
                }
                chooseKesou = isChecked
                if (chooseYanhou && chooseQichuan && chooseBizi && chooseToutong && chooseShentong && user.checkToutongFinish()) {
                    Event.post(ReleaseNextSurveyEvent(true))
                }
            }
            radio_button_tanshao_nankechu.setOnCheckedChangeListener { buttonView, isChecked ->
                user.nanKechu = isChecked
                if (user.youqichuan) {
                    radio_button_ketan_qingxi.clearCheck()
                    radio_button_ketan_huangchou.clearCheck()
                    radio_button_baitan_yikechu.clearCheck()
                    radio_button_wutan.clearCheck()
                }
                chooseKesou = isChecked
                if (chooseYanhou && chooseQichuan && chooseBizi && chooseToutong && chooseShentong && user.checkToutongFinish()) {
                    Event.post(ReleaseNextSurveyEvent(true))
                }
            }
            radio_button_wutan.setOnCheckedChangeListener { buttonView, isChecked ->
                user.wutanGanke = isChecked
                if (user.youqichuan) {
                    radio_button_ketan_qingxi.clearCheck()
                    radio_button_ketan_huangchou.clearCheck()
                    radio_button_baitan_yikechu.clearCheck()
                    radio_button_tanshao_nankechu.clearCheck()
                    radio_button_wutan.clearCheck()
                }
                chooseKesou = isChecked
                if (chooseYanhou && chooseQichuan && chooseBizi && chooseToutong && chooseShentong && user.checkToutongFinish()) {
                    Event.post(ReleaseNextSurveyEvent(true))
                }
            }

        }

        //如果有头痛身痛
        if (!user.toutongJiroutengtongGujietengtong) {
            tv_title_toutong.visibility = View.GONE
            radio_button_diandingtong.visibility = View.GONE
            radio_button_toutong_zouce.visibility = View.GONE
            radio_button_toutong_youce.visibility = View.GONE
            radio_button_toutong_hounao.visibility = View.GONE
            radio_button_etoutong.visibility = View.GONE
            radio_button_meilenggutong.visibility = View.GONE
            radio_button_toutong_wuweizhi.visibility = View.GONE
            chooseToutong = true
            radio_button_jiroutengtong.visibility = View.GONE
            radio_button_jingxiangtengtong.visibility = View.GONE
            radio_button_guanjietengtong.visibility = View.GONE
            radio_button_shentong_wuweizhi.visibility = View.GONE
            chooseShentong = true
        } else {
            //头痛
            fun toutongCheckReleaseNext() {
                chooseToutong = true
                log {
                    """
                        bizi: $chooseBizi
                        toutong: $chooseToutong
                        kesou: $chooseKesou
                        checkToutong: ${user.checkToutongFinish()}
                    """.trimIndent()
                }
                if (chooseYanhou && chooseShentong && chooseQichuan && chooseKesou && chooseBizi && user.checkToutongFinish()) {
                    Event.post(ReleaseNextSurveyEvent(true))
                } else {
                    Event.post(ReleaseNextSurveyEvent(false))
                }
            }
            //监听变量，如果第一被触发之后，马上更换空的监听器以便不会被触发
            val listener = CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) {
                    user.clearToutong()
                    radio_button_diandingtong.clearCheck()
                    radio_button_toutong_zouce.clearCheck()
                    radio_button_toutong_youce.clearCheck()
                    radio_button_toutong_hounao.clearCheck()
                    radio_button_etoutong.clearCheck()
                    radio_button_meilenggutong.clearCheck()
                    radio_button_toutong_wuweizhi.setOnCheckedChangeListener { buttonView, isChecked ->
                        //保留基本的判断
                        user.wuweizhiToutong = isChecked
                        toutongCheckReleaseNext()
                    }
                    radio_button_toutong_wuweizhi.isChecked = isChecked
                }
                user.wuweizhiToutong = isChecked
                toutongCheckReleaseNext()
            }
            radio_button_toutong_wuweizhi.setOnCheckedChangeListener(listener)

            radio_button_diandingtong.setOnCheckedChangeListener { buttonView, isChecked ->
                radio_button_toutong_wuweizhi.setOnCheckedChangeListener(listener)
                radio_button_toutong_wuweizhi.clearCheck()
                user.diandingtong = isChecked
                toutongCheckReleaseNext()
            }
            radio_button_toutong_zouce.setOnCheckedChangeListener { buttonView, isChecked ->
                radio_button_toutong_wuweizhi.setOnCheckedChangeListener(listener)
                radio_button_toutong_wuweizhi.clearCheck()
                user.zuoceToutong = isChecked
                if (isChecked) {
                    radio_button_toutong_youce.clearCheck()
                }
                toutongCheckReleaseNext()
            }
            radio_button_toutong_youce.setOnCheckedChangeListener { buttonView, isChecked ->
                radio_button_toutong_wuweizhi.setOnCheckedChangeListener(listener)
                radio_button_toutong_wuweizhi.clearCheck()
                user.youceToutong = isChecked
                if (isChecked) {
                    radio_button_toutong_zouce.clearCheck()
                }
                toutongCheckReleaseNext()
            }

            radio_button_toutong_hounao.setOnCheckedChangeListener { buttonView, isChecked ->
                radio_button_toutong_wuweizhi.setOnCheckedChangeListener(listener)
                radio_button_toutong_wuweizhi.clearCheck()
                user.hounaotong = isChecked
                toutongCheckReleaseNext()
            }
            radio_button_etoutong.setOnCheckedChangeListener { buttonView, isChecked ->
                radio_button_toutong_wuweizhi.setOnCheckedChangeListener(listener)
                radio_button_toutong_wuweizhi.clearCheck()
                user.etoutong = isChecked
                toutongCheckReleaseNext()
            }
            radio_button_meilenggutong.setOnCheckedChangeListener { buttonView, isChecked ->
                radio_button_toutong_wuweizhi.setOnCheckedChangeListener(listener)
                radio_button_toutong_wuweizhi.clearCheck()
                user.meilenggutong = isChecked
                toutongCheckReleaseNext()
            }

            //身痛
            fun shentongCheckReleaseNext() {
                chooseShentong = true
                if (chooseYanhou && chooseToutong && chooseQichuan && chooseKesou && chooseBizi && user.checkToutongFinish()) {
                    Event.post(ReleaseNextSurveyEvent(true))
                } else {
                    Event.post(ReleaseNextSurveyEvent(false))
                }
            }
            radio_button_jiroutengtong.setOnCheckedChangeListener { buttonView, isChecked ->
                user.jiroutengtong = isChecked
                if (isChecked) {
                    radio_button_shentong_wuweizhi.clearCheck()
                    shentongCheckReleaseNext()
                }
                chooseShentong = isChecked
            }
            radio_button_jingxiangtengtong.setOnCheckedChangeListener { buttonView, isChecked ->
                user.jingxiangtengtong = isChecked
                if (isChecked) {
                    radio_button_shentong_wuweizhi.clearCheck()
                    shentongCheckReleaseNext()
                }
                chooseShentong = isChecked
            }
            radio_button_guanjietengtong.setOnCheckedChangeListener { buttonView, isChecked ->
                user.guanjietengtong = isChecked
                if (isChecked) {
                    radio_button_shentong_wuweizhi.clearCheck()
                    shentongCheckReleaseNext()
                }
                chooseShentong = isChecked
            }
            radio_button_shentong_wuweizhi.setOnCheckedChangeListener { buttonView, isChecked ->
                user.shentongwuzhiwei = isChecked
                if (isChecked) {
                    radio_button_jiroutengtong.clearCheck()
                    radio_button_jingxiangtengtong.clearCheck()
                    radio_button_guanjietengtong.clearCheck()
                    shentongCheckReleaseNext()
                }
                chooseShentong = isChecked
            }
        }

        //如果流鼻涕
        if (!user.pentiBisaiLiuti) {
            radio_group_liuti.visibility = View.GONE
            radio_button_bise.visibility = View.GONE
            radio_button_penti.visibility = View.GONE
            radio_button_biganzao.setOnCheckedChangeListener { buttonView, isChecked ->
                user.clearLiuti()
                user.biganBizao = isChecked
                chooseBizi = true
                log {
                    """
                        bizi: $chooseBizi
                        toutong: $chooseToutong
                        kesou: $chooseKesou
                        checkToutong: ${user.checkToutongFinish()}
                    """.trimIndent()
                }
                if (chooseYanhou && chooseQichuan && chooseKesou && chooseToutong && chooseShentong && user.checkToutongFinish()) {
                    Event.post(ReleaseNextSurveyEvent(true))
                }
            }
        } else {
            radio_button_biganzao.visibility = View.GONE
            radio_group_liuti.setOnCheckedChangeListener { group, checkedId ->
                user.clearLiuti()
                when(checkedId) {
                    R.id.radio_button_biti_qing -> user.bitiQing = true
                    R.id.radio_button_biti_bai -> user.bitiBai = true
                    R.id.radio_button_biti_huang -> user.bitiHuang = true
                }
                log {
                    """
                        bizi: $chooseBizi
                        toutong: $chooseToutong
                        kesou: $chooseKesou
                        checkToutong: ${user.checkToutongFinish()}
                    """.trimIndent()
                }
                chooseBizi = true
                if (chooseYanhou && chooseQichuan && chooseKesou && chooseToutong && chooseShentong && user.checkToutongFinish()) {
                    Event.post(ReleaseNextSurveyEvent(true))
                }
            }
        }
        radio_button_bise.setOnCheckedChangeListener { buttonView, isChecked ->
            user.bise = isChecked
            chooseBizi = isChecked
            if (chooseYanhou && chooseQichuan && chooseKesou && chooseToutong && chooseShentong && user.checkToutongFinish()) {
                Event.post(ReleaseNextSurveyEvent(true))
            }
        }
        radio_button_penti.setOnCheckedChangeListener { buttonView, isChecked ->
            user.penti = isChecked
            chooseBizi = isChecked
            if (chooseYanhou && chooseQichuan && chooseKesou && chooseToutong && chooseShentong && user.checkToutongFinish()) {
                Event.post(ReleaseNextSurveyEvent(true))
            }
        }

        //咽干咽痛咽痒
        if (user.yanganYantongYanyang) {
            radio_button_yangan.setOnCheckedChangeListener { buttonView, isChecked ->
                user.yangan = isChecked
                if (isChecked) chooseYanhouCount++ else chooseYanhouCount--
            }
            radio_button_yantong.setOnCheckedChangeListener { buttonView, isChecked ->
                user.yantong = isChecked
                if (isChecked) chooseYanhouCount++ else chooseYanhouCount--
            }
            radio_button_yanyang.setOnCheckedChangeListener { buttonView, isChecked ->
                user.yanyang = isChecked
                if (isChecked) chooseYanhouCount++ else chooseYanhouCount--
            }
        } else {
            tv_title_yanhou.visibility = View.GONE
            radio_button_yangan.visibility = View.GONE
            radio_button_yantong.visibility = View.GONE
            radio_button_yanyang.visibility = View.GONE
            chooseYanhou = true
        }
    }

    override fun lazyInit() {
    }

    override fun onVisible() {
        Event.post(ReleaseNextSurveyEvent(false))
        if (chooseYanhou && chooseQichuan && chooseKesou && chooseToutong && chooseShentong && chooseBizi && user.checkToutongFinish()) {
            Event.post(ReleaseNextSurveyEvent(true))
        }
    }
}